package utils 
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class DebugWindow extends Sprite
	{
		private var _btnClose:Sprite;
		private var _text:TextField;
		private var _textHeader:TextField;
		
		public function DebugWindow() 
		{
			addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
		}
		
		private function handleAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
			initWindow();
		}
		
		private function initWindow():void 
		{
			var windowWidth:Number = 280;
			var windowHeight:Number = 120;
			var borderColor:uint = 0x666666;
			var windowColor:uint = 0xE5E5E5;
			var headerColor:uint = 0x999999;
			var headerHeight:Number = 25;
			var lineThickNess:Number = 1.5;
			
			var gr:Graphics = this.graphics;
			gr.lineStyle(lineThickNess, borderColor);
			gr.beginFill(windowColor, 1);
			gr.drawRect(0, 0, windowWidth, windowHeight);
			gr.endFill();
			
			var headerBar:Sprite = new Sprite();
			gr = headerBar.graphics;
			gr.lineStyle(lineThickNess, borderColor);
			gr.beginFill(headerColor, 1);
			gr.drawRect(0, 0, windowWidth, headerHeight);
			gr.endFill();
			headerBar.x = 0;
			headerBar.y = 0;
			addChild(headerBar);
			headerBar.addEventListener(MouseEvent.MOUSE_DOWN, handleHeaderBarDown);
			headerBar.addEventListener(MouseEvent.MOUSE_UP, handleHeaderBarUp);
			
			var closeBtnColor:uint = 0xE5E5E5;
			var closeButton:Sprite = new Sprite();
			var scaleFactor:Number = 0.6;
			gr = closeButton.graphics;
			gr.lineStyle(lineThickNess, borderColor);
			gr.beginFill(closeBtnColor, 1);
			gr.drawRect(0, 0, headerHeight, headerHeight);
			gr.endFill();
			gr.moveTo(headerHeight - headerHeight*scaleFactor, headerHeight - headerHeight*scaleFactor);
			gr.lineTo(headerHeight*scaleFactor, headerHeight*scaleFactor);
			gr.moveTo(headerHeight*scaleFactor, headerHeight - headerHeight*scaleFactor);
			gr.lineTo(headerHeight - headerHeight*scaleFactor, headerHeight*scaleFactor);
			closeButton.x = windowWidth - headerHeight;
			closeButton.y = 0;
			closeButton.buttonMode = true;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, handleCloseClick);
			
			var textPadding:Number = lineThickNess + 1.5;
			_text = new TextField();
			addChild(_text);
			_text.x = textPadding;
			_text.y = headerHeight + textPadding;
			_text.width = windowWidth - textPadding * 2;
			_text.wordWrap = true;
			
			_textHeader = new TextField();
			_textHeader.x = textPadding;
			_textHeader.y = textPadding;
			_textHeader.width = windowWidth - textPadding * 2 - headerHeight;
			_textHeader.wordWrap = false;
			_textHeader.mouseEnabled = false;
			addChild(_textHeader);
			
			showMessages(ErrorsCollector.getInstance().errors);
			addEventListener(Event.ADDED_TO_STAGE, handleAddedForShow);
		}
		
		private function handleAddedForShow(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, handleAddedForShow);
			addEventListener(Event.REMOVED_FROM_STAGE, handleRemovedForHide);
			showMessages(ErrorsCollector.getInstance().errors);
		}
		
		private function handleRemovedForHide(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, handleRemovedForHide);
			addEventListener(Event.ADDED_TO_STAGE, handleAddedForShow);
		}
		
		private function handleCloseClick(e:MouseEvent):void 
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		private function handleHeaderBarUp(e:MouseEvent):void 
		{
			this.stopDrag();
		}
		
		private function handleHeaderBarDown(e:MouseEvent):void 
		{
			this.startDrag();
		}
		
		public function showMessages(msgArr:Vector.<String>):void
		{
			_textHeader.text = "Total messages: " + msgArr.length;
			_text.text = "";
			for (var i:int = 0; i < msgArr.length; i++)
			{
				_text.text = _text.text.concat(i.toString() + ") " + msgArr[i] + "\n");
			}
		}
	}

}